import utils from '@/mixins/utils.js';
import { resolve } from 'url';
export default {
	name: 'YTJ_device',
	mixins: [utils],
	data() {
		return {
			deviceConfigList: [],
			currentEditItem: null,
			parkConfigInfo: null
		};
	},
	created() {
		this.getParkConfigInfoByUUID();
		this.getDeviceConfigList();
	},
	methods: {
		getParkConfigInfoByUUID() {
			var self = this;
			API.fetchGet('/operatorPark/getOperatorParkConfigInfo', {
				parkID: this.$route.query.parkId
			}).then(
				res => {
					this.parkConfigInfo = res.data;
				},
				err => {}
			);
		},
		getDeviceConfigList() {
			const self = this;
			API.fetchGet('/operatorPark/getDeviceConfigList', {
				parkID: this.$route.query.parkId
			}).then(
				res => {
					if (!this.deviceConfigList.length && res.data.length) {
						this.deviceConfigList = res.data.map(item => {
							item.uiOpt = {
								isEdit: false,
								isShowDetail: false
							};
							return item;
						});
					}

					if (this.deviceConfigList.length && res.data.length) {
						res.data.forEach(item => {
							const same_item = this.deviceConfigList.filter(sitem => {
								return sitem.channelConfigVo.channelID === item.channelConfigVo.channelID;
							});
							item.uiOpt = same_item[0].uiOpt;
							item = Object.assign(same_item[0], item);
						});
						this.deviceConfigList = res.data;
					}
				},
				err => {}
			);
		},
		changeDeviceIpMode(newIpMode) {
			let modeName;
			if (newIpMode === 0) {
				modeName = '自动获取';
			}

			if (newIpMode === 1) {
				modeName = '手动设置';
			}
			this.$confirm(`切换后，当前停车场设备的IP获取模式将切换为${modeName}`, '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				customClass: 'mgr-msg-box',
				confirmButtonClass: 'el-button el-button--primary'
			})
				.then(() => {
					API.fetchPost('/operatorPark/setParkDeviceModel', {
						deviceConfigVoList: this.deviceConfigList,
						deviceModel: newIpMode,
						parkID: this.$route.query.parkId
					}).then(
						res => {
							if (res.status === 1 && res.resultCode === 0) {
								this.showToast('IP模式修改成功', 1, 1500);
								this.deviceConfigList.splice(index, 1);
								this.getDeviceConfigList();
							} else {
								this.showToast('IP模式修改失败', 2, 1500);
								this.parkConfigInfo.parkCloudDetailVo.deviceModel = newIpMode === 0 ? 1 : 0;
							}
						},
						err => {
							this.showToast('IP模式修改失败', 2, 1500);
							this.parkConfigInfo.parkCloudDetailVo.deviceModel = newIpMode === 0 ? 1 : 0;
						}
					);
				})
				.catch(() => {
					this.parkConfigInfo.parkCloudDetailVo.deviceModel = newIpMode === 0 ? 1 : 0;
				});
		},
		editDeviceItem(item) {
			this.currentEditItem = JSON.parse(JSON.stringify(item));
			this.deviceConfigList.forEach(item => {
				item.uiOpt.isEdit = false;
			});
			item.uiOpt.isEdit = true;
		},
		cancelEdit(list, index) {
			list.splice(index, 1, this.currentEditItem);
		},
		changeDeviceItem(item) {
			API.fetchPost('/operatorPark/processDevice/1', item).then(
				res => {
					if (res.status === 1) {
						this.showToast('设备修改成功', 1, 1500);
					} else {
						this.showToast(res.message, 1, 2000);
					}
					item.uiOpt.isEdit = false;
					this.getDeviceConfigList();
				},
				err => {
					console.log(err);
				}
			);
		},
		getTideChannelStatus(curItem) {
			let params = {
				parkID: curItem.channelConfigVo.parkGlobalID,
				channelID: curItem.channelConfigVo.channelID
			};
			return API.fetchGet('/operatorPark/checkChannel', params);
		},
		delDevice(item, index) {
			this.getTideChannelStatus(item).then(res => {
				if (res.status === 0 && res.resultCode === 0) return;

				if (res.status === 1 && (res.resultCode === -2 || res.resultCode === -3)) {
					this.showToast('此设备已绑定潮汐通道，解绑后可删除', 1, 2000);
					return;
				}

				this.$confirm('您确定要删除本设备？', '删除设备', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					customClass: 'mgr-msg-box',
					confirmButtonClass: 'el-button el-button--danger'
				}).then(() => {
					API.fetchPost('/operatorPark/processDevice/2', item).then(
						res => {
							if (res.status === 1) {
								this.showToast('删除设备成功', 1, 1500);
								this.getDeviceConfigList();
							} else {
								this.showToast('删除设备失败', 2, 1500);
							}
							item.uiOpt.isEdit = false;
						},
						err => {
							console.log(err);
						}
					);
				});
			});
		},
		//进入设备后台
		toDeviceBackend(parkID, ytjId) {
			let _this = this;
			let params = {
				parkID: parkID,
				ytjId: ytjId
			};
			API.fetchGet('operatorPark/get666BackendUrl', params).then(
				res => {
					let data = res.data;
					if (data && data.url) {
						window.open(data.url, '_blank');
					} else {
						_this.showToast('设备暂不支持该功能', 0);
					}
				},
				err => {}
			);
		}
	},
	computed: {
		curIpType() {
			return this.parkConfigInfo && this.parkConfigInfo.parkCloudDetailVo.deviceModel === 0
				? '自动获取'
				: '手动设置';
		}
	}
};
